import 'package:demo1/pages/cart/index.dart';
import 'package:demo1/pages/category/index.dart';
import 'package:demo1/pages/home/index.dart';
import 'package:demo1/pages/mine/index.dart';
import 'package:flutter/material.dart';

class TabBarPage extends StatefulWidget {
  const TabBarPage({super.key});

  @override
  State<TabBarPage> createState() => _TabBarPageState();
}

class _TabBarPageState extends State<TabBarPage> {
  // 记录当前索引
  int currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: IndexedStack(
          index: currentIndex,
          children: const [HomePage(), CategoryPage(), CartPage(), MinePage()],
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        items: barItemList(),
        currentIndex: currentIndex,
        selectedItemColor: Colors.red,
        unselectedItemColor: Colors.black,
        onTap: (index) {
         setState(() {
             currentIndex = index;
          });
        },
      ),
    );
  }

  List<BottomNavigationBarItem> barItemList() {
    List<BottomNavigationBarItem> items = [];
    items.add(
      BottomNavigationBarItem(
          icon: Image.asset(
            'tabbar/home.png',
            width: 32,
            height: 32,
          ),
          activeIcon: Image.asset(
            'tabbar/home-active.png',
            width: 32,
            height: 32,
          ),
          label: '首页'),
    );
    items.add(
      BottomNavigationBarItem(
          icon: Image.asset(
            'tabbar/cate.png',
            width: 32,
            height: 32,
          ),
          activeIcon: Image.asset(
            'tabbar/cate-active.png',
            width: 32,
            height: 32,
          ),
          label: '分类'),
    );
    items.add(
      BottomNavigationBarItem(
          icon: Image.asset(
            'tabbar/cart.png',
            width: 32,
            height: 32,
          ),
          activeIcon: Image.asset(
            'tabbar/cart-active.png',
            width: 32,
            height: 32,
          ),
          label: '购物车'),
    );
    items.add(
      BottomNavigationBarItem(
          icon: Image.asset(
            'tabbar/user.png',
            width: 32,
            height: 32,
          ),
          activeIcon: Image.asset(
            'tabbar/user-active.png',
            width: 32,
            height: 32,
          ),
          label: '我的'),
    );
    return items;
  }
}